package cn.wlj.origin.topic;

import cn.wlj.origin.ConnectUtil;
import cn.wlj.origin.Constant;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;

/**
 * @ClassName InsertReceiver
 * @Description TODO
 * @Author WLJ
 * @Date 2024/1/15 21:02
 * @Version 1.0
 */
public class UpdateDeleteReceiver {
    public static void main(String[] args) throws Exception {
        // 交换机名
        String exchangeName = Constant.Topic.EXCHANGE_NAME;
        // 队列名
        String queueName = Constant.Topic.UPDATE_DELETE_QUEUE_NAME;
        // 通配符
        String updateFlag = Constant.Topic.UPDATE_FLAG;
        // 删除通配符
        String deleteFlag = Constant.Topic.DELETE_FLAG;

        // 获取连接
        Connection connection = ConnectUtil.getConnection();
        // 获取通道
        Channel channel = connection.createChannel();
        // 队列绑定
        channel.queueBind(queueName, exchangeName, updateFlag);
        channel.queueBind(queueName, exchangeName, deleteFlag);

        // 接收的回调函数
        DeliverCallback deliverCallback = ((s, delivery) ->{
            //接收消息的处理逻辑
            String msg = new String(delivery.getBody(),"UTF-8");
            System.out.println("【" + queueName + "】接收消息："+msg);
        });
        // 接收错误的回调
        CancelCallback cancelCallback = (s -> {
            System.out.println("【" + queueName + "】接收消息异常");
        });
        channel.basicConsume(queueName, deliverCallback, cancelCallback);
    }
}
